package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ReportprojectMapper;
import com.hbnu.system.model.entity.Porproject_registration;
import com.hbnu.system.model.entity.Report;
import com.hbnu.system.model.entity.Reportproject;
import com.hbnu.system.model.entity.Tranproject_registration;
import com.hbnu.system.service.IPorproject_registrationService;
import com.hbnu.system.service.IReportService;
import com.hbnu.system.service.IReportprojectService;
import com.hbnu.system.service.ITranproject_registrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class ReportprojectServiceImpl extends ServiceImpl<ReportprojectMapper, Reportproject> implements IReportprojectService {

//    @Autowired
//    IPorproject_midinspectionService iPorproject_midinspectionService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;
    @Autowired
    ITranproject_registrationService iTranproject_registrationService;


    @Autowired
    IReportService iReportService;
    @Override
    public boolean addreportProject(Reportproject reportproject) {
        //署名单位和
        if(StrUtil.isEmpty(reportproject.getProjectID()) || StrUtil.isBlank(reportproject.getProjectID())){
            throw new BaseException("请输入项目编号!");
        }
        if(StrUtil.isEmpty(reportproject.getProjectName()) || StrUtil.isBlank(reportproject.getProjectName())){
            throw new BaseException("请输入项目名称!");
        }
        if(StrUtil.isEmpty(reportproject.getHead()) || StrUtil.isBlank(reportproject.getHead())){
            throw new BaseException("请输入项目负责人!");
        }

        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                .eq(Porproject_registration::getProjectId, reportproject.getProjectID())
                .eq(Porproject_registration::getProjectName, reportproject.getProjectName()));

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                .eq(Tranproject_registration::getProjectId, reportproject.getProjectID())
                .eq(Tranproject_registration::getProjectName, reportproject.getProjectName()));

        if(Objects.isNull(porproject) && Objects.isNull(tranproject)){
            throw new BaseException("不存在该项目!");
        }

        if(Objects.nonNull(porproject)){
            if(Objects.isNull(porproject.getStartTime())) {
                reportproject.setStartTime(porproject.getStartTime());
            }
            if(Objects.isNull(porproject.getEndTime())) {
                reportproject.setEndTime(porproject.getEndTime());
            }
        }else {
            if(Objects.isNull(tranproject.getStartTime())) {
                reportproject.setStartTime(tranproject.getStartTime());
            }
            if(Objects.isNull(tranproject.getEndTime())) {
                reportproject.setEndTime(tranproject.getEndTime());
            }
        }

        //把对应的论文Paper上的字段“关联项目”也要进行修改
        Report report = iReportService.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID, reportproject.getReportID()));
        if (Objects.isNull(report)){
            throw new BaseException("报告不存在!");
        }
//        //首先把署名单位进行赋值
//        reportproject.setSignedUnit();

        if(Objects.isNull(report.getAssociatedProjects())) {
            //得到原来的联合项目名单
            String associated_projects = report.getAssociatedProjects();
            if (report.getAssociatedProjects().contains(reportproject.getProjectName())) {
                throw new BaseException("不可重复添加!");
            }
            if (associated_projects.isEmpty() || associated_projects.equals(" ")) {
                report.setAssociatedProjects(reportproject.getProjectName());
            } else {
                report.setAssociatedProjects(associated_projects + "、" + reportproject.getProjectName());
            }
        }else{
            report.setAssociatedProjects(reportproject.getProjectName());
        }
        //把相应论文paper进行修改使用update接口 更新它的相关项目这一字段
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ReportID",report.getReportID());
        iReportService.update(report,updateWrapper);
//        System.out.println("到此4444444444444444444444444444444444444444");
//        baseMapper.update(paper);

        return baseMapper.insert(reportproject) == 1;
    }

    @Override
    public boolean updatereportProject(Reportproject reportproject) {
        return false;
    }

    @Override
    public boolean removereportProject(String reportProjectID) {
        return false;
    }

    @Override
    public List<Reportproject> getReportProjectByReportID(String reportID) {
        return null;
    }
}
